﻿using System;
using System.Drawing;
using System.Windows.Forms;
using HHControls.CreditEducation.Properties;

namespace HHControls.CreditEducation
{
    public class HHArrowButton : Button
    {
        #region Fields

        private Bitmap _currentImage;

        #endregion

        #region Constructor

        public HHArrowButton()
        {
            Initialize();
            InitializeButton();
        }

        private void Initialize()
        {
            ForeColor = Color.White;
            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 0;
            FlatAppearance.MouseDownBackColor = Color.Transparent;
            FlatAppearance.MouseOverBackColor = Color.Transparent;
            BackColor = Color.Transparent;
            Font = new Font("Tahoma", (float)Height / 2, FontStyle.Bold, GraphicsUnit.Point);
        }

        private void InitializeButton()
        {
            ImageNormal = new Bitmap(Resources.button_hover, Width, Height);
            ImageHover = new Bitmap(Resources.button_normal, Width, Height);
            ImageClicked = new Bitmap(Resources.button_clicked, Width, Height);
            _currentImage = ImageNormal;
        }

        #endregion

        #region Properties

        public Bitmap ImageNormal { get; set; }
        public Bitmap ImageHover { get; set; }
        public Bitmap ImageClicked { get; set; }

        #endregion

        #region Paint

        private void DrawImage(Graphics graphics)
        {
            graphics.DrawImage(_currentImage, 0, 0);

            var layoutRectangle = new Rectangle(0, 0, Width, Height);
            var stringFormat = new StringFormat
                                   {
                                       Alignment = StringAlignment.Center,
                                       LineAlignment = StringAlignment.Center
                                   };
            graphics.DrawString(Text, Font, new SolidBrush(ForeColor), layoutRectangle, stringFormat);
        }

        #endregion

        #region Override Methods

        protected override void OnSizeChanged(EventArgs e)
        {
            InitializeButton();
            Invalidate();
            base.OnSizeChanged(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawImage(e.Graphics);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            _currentImage = ImageClicked;
            Invalidate();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            _currentImage = ImageHover;
            Invalidate();
            base.OnMouseUp(e);
        }

        protected override void OnMouseHover(EventArgs e)
        {
            _currentImage = ImageHover;
            Invalidate();
            base.OnMouseHover(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            _currentImage = ImageNormal;
            Invalidate();
            base.OnMouseLeave(e);
        }

        #endregion
    }
}